<?php

namespace Bundle\ModuleBundle;

use Bundle\ModuleBundle\DependencyInjection\ModuleExtension;

use Symfony\Component\HttpKernel\Bundle\Bundle;

use Symfony\Component\Finder\Finder;

class ModuleBundle extends Bundle
{
    public function buildContainer(ParameterBagInterface $parameterBag)
    {
        ContainerBuilder::registerExtension(new ModuleExtension());
    }

    public function boot()
    {
        $moduleManager = new ModuleManager($this->container);
        $this->container['fireice']->setModuleManager($moduleManager);

        $dirs = array(
            $this->getNamespacePrefix() . '\\' . $this->getName() . '\\' . 'Plugins' . '\\'
                => realpath($this->getPath() . '/Plugins/')
        );

        $moduleManager->setPluginsSearchPath($dirs);

        // DIRTY FIX FOR 'console' run -> caused by bug in doctrine StaticPHPDriver
        if(php_sapi_name() == 'cli')
        {
            return;
        }

        // injection for modules entities
        $emConfig = $this->container->getDoctrine_ORM_EntityManagerService()
                ->getConfiguration();

        $driver = new \Doctrine\ORM\Mapping\Driver\StaticPHPDriver(
            realpath($this->container->getParameter('module.path_for_models')));

        $emConfig->getMetadataDriverImpl()->addDriver($driver, $this->container->getParameter('module.namespace_for_models'));

        $this->container->get('logger')->info('Injected Doctrine Driver for loading models: namespace = "' . $this->container->getParameter('module.namespace_for_models') .
                '" path = "' . realpath($this->container->getParameter('module.path_for_models')) . '"');
        
    }
}